package vista;

import Bls.AutorBlLocal;
import Modelo.Autor;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

@ManagedBean
@SessionScoped
public class AutorBean {
  
    @EJB
    private AutorBlLocal autorBl;
     
  private List<Autor> lista;
  private Autor author;
  private String accion;

    public AutorBean() {
        author=new Autor();
    }

    public List<Autor> getLista() {
        lista=autorBl.getAll();
        System.out.println(lista);
        
        return lista;
    }
     public Autor getAutor() {
        return author;
    }

    public void setAutor(Autor author) {
        this.author = author;
    }

public String procesarAccion(){
    if(accion.equalsIgnoreCase("nuevo")){
    autorBl.register(author);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
    autorBl.ModiFy(author);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
    autorBl.delete(author);
    }
    }
    }
    accion="";
    return "Autor";

}    
 
public void prepararNuevo(ActionEvent e){
        author=new Autor(); 
        accion="Nuevo";
}
public void prepararEditar(ActionEvent evt){
        System.out.println(evt.getComponent().getClientId());
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        System.out.println("el numero es:"+ index);
        author=lista.get(index);
        accion="Editar";               
}
public void prepararEliminar(ActionEvent ev){
        int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
        author=lista.get(index);
        accion="Eliminar";
}
}
